$(function(){
	updatepw_onload.begin();
});
updatepw = {
		update : function(){
			var old_pw = $("#old_passwd").val();
			old_pw = $.trim(old_pw);
			var new_pw = $("#new_passwd").val();
			new_pw = $.trim(new_pw);
			var con_pw = $("#con_passwd").val();
			con_pw = $.trim(con_pw);

			if(''==old_pw || old_pw.length<6){
				alert('旧密码不能是空,密码长度不能够小于6位');
				return;
			}
			if(''==new_pw || new_pw.length<6){
				alert('新密码不能是空,密码长度不能够小于6位');
				return;
			}
			
			if(new_pw != con_pw){
				alert('新密码与确认密码不一致');
				return;
			}
			
			var value = old_pw+" "+new_pw; 
			var key = $.cookie("k");
			value = updatepw.encrypt(value,key);
			$.ajax( {
				data : 'value='+value,
				dataType : "json",
				type : "POST",
				async : true,
				url : "/auth/user!updateUserPassword",
				success : function(data) {
					if('ok' == data.state){
						alert('修改密码成功');
						window.location.href=global_parameter.login_page;
					}else{
						alert(data.msg);
						//$("#msg").html(data.msg);
					}
				} 
			});
		},
		encrypt : function (word,key){
	    	 var key = CryptoJS.enc.Utf8.parse(key);    
	         var iv  = CryptoJS.enc.Utf8.parse('0102030405060755');    
	         var srcs = CryptoJS.enc.Utf8.parse(word);   
	         var encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: iv,mode:CryptoJS.mode.CBC});   
	         return encrypted.toString();   
    	},
    	stringToHex : function (str){
    			var val="";
    			for(var i = 0; i < str.length; i++){
    				if(val == ""){
    					val = str.charCodeAt(i).toString(16);
    				}else{
    					val += str.charCodeAt(i).toString(16);
    				}
    			}
    		return val;
    	}
};

updatepw_onload = {
		begin : function(){
			var un = $.cookie("un");
			$("#username").val(un);
		}
};

